import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";
import store from "@/store";
export const routesLayoutChild: Array<RouteRecordRaw> = [
  {
    path: "/home",
    name: "/home",
    component: () =>
      import(/* webpackChunkName: "home" */ "@/views/home/indexView.vue"),
    meta: {
      title: "首页",
    },
  },
  {
    path: "/friend",
    name: "/friend",
    component: () =>
      import(/* webpackChunkName: "friend" */ "@/views/friend/indexView.vue"),
    meta: {
      title: "车友圈",
    },
  },
  {
    path: "/select/car",
    name: "/selectCar",
    component: () =>
      import(
        /* webpackChunkName: "selectCar" */ "@/views/selectCar/indexView.vue"
      ),
    meta: {
      title: "选车",
    },
  },
  {
    path: "/old/car",
    name: "/oldCar",
    component: () =>
      import(/* webpackChunkName: "oldCar" */ "@/views/oldCar/indexView.vue"),
    meta: {
      title: "二手车",
    },
  },
  {
    path: "/serve",
    name: "/serve",
    component: () =>
      import(/* webpackChunkName: "serve" */ "@/views/serve/indexView.vue"),
    meta: {
      title: "服务",
    },
  },
];

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "/layout",
    redirect: "/select/car",
    component: () =>
      import(/* webpackChunkName: "layout" */ "@/views/layout/indexView.vue"),
    children: routesLayoutChild,
  },
  {
    path: "/car/:id",
    name: "/carDetail",
    component: () =>
      import(
        /* webpackChunkName: "carDetail" */ "@/views/detail/indexView.vue"
      ),
    props: true,
  },
  {
    path: "/login",
    name: "/login",
    component: () =>
      import(/* webpackChunkName: "login" */ "@/views/login/indexView.vue"),
  },
  {
    path: "/post/:id",
    name: "/post",
    component: () =>
      import(/* webpackChunkName: "post" */ "@/views/post/indexView.vue"),
    props: true,
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

// router.beforeEach((to, from, next) => {
//   const token = store.state.userInfo.token

//   if(token) {
//     // 已登录
//     if(to.path === '/login') {
//       // 已经登录  访问登录页面
//     }
//   } else {
//     // 未登录
//   }
// })
export default router;
